package utils

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"

	"golang.org/x/crypto/bcrypt"
)

// 密码加密处理
func PasswordHash(password string) (string, error) {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	return string(bytes), err
}

// 密码解密处理
func PasswordVerify(password, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
}

// func main() {
// 	password := "123456"
// 	hash, _ := PasswordHash(password)

// 	fmt.Println("密码:", password)
// 	fmt.Println("hash:", hash)

// 	match := PasswordVerify(password, hash)
// 	fmt.Println("验证:", match)
// }

// md5加密
func EncodeMD5(value string) string {
	m := md5.New()
	m.Write([]byte(value))
	return hex.EncodeToString(m.Sum(nil))
}
